package com.ruoyi.commerce.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.commerce.domain.bo.CommerceProductsBo;
import com.ruoyi.commerce.domain.vo.CommerceProductsVo;
import com.ruoyi.commerce.domain.CommerceProducts;
import com.ruoyi.commerce.mapper.CommerceProductsMapper;
import com.ruoyi.commerce.service.ICommerceProductsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-31
 */
@RequiredArgsConstructor
@Service
public class CommerceProductsServiceImpl implements ICommerceProductsService {

    private final CommerceProductsMapper baseMapper;

    /**
     * 查询商品
     */
    @Override
    public CommerceProductsVo queryById(Long productId){
        return baseMapper.selectVoById(productId);
    }

    /**
     * 查询商品
     */
    @Override
    public List<CommerceProductsVo> queryByIds(Collection<Long> productIds){
        return baseMapper.selectVoBatchIds(productIds);
    }

    /**
     * 查询商品列表
     */
    @Override
    public TableDataInfo<CommerceProductsVo> queryPageList(CommerceProductsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CommerceProducts> lqw = buildQueryWrapper(bo);
        Page<CommerceProductsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品列表
     */
    @Override
    public List<CommerceProductsVo> queryList(CommerceProductsBo bo) {
        LambdaQueryWrapper<CommerceProducts> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CommerceProducts> buildQueryWrapper(CommerceProductsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CommerceProducts> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getProductName()), CommerceProducts::getProductName, bo.getProductName());
        lqw.like(StringUtils.isNotBlank(bo.getProductDescription()), CommerceProducts::getProductDescription, bo.getProductDescription());
        lqw.between(params.get("beginProductPrice") != null && params.get("endProductPrice") != null,
            CommerceProducts::getProductPrice ,params.get("beginProductPrice"), params.get("endProductPrice"));
        lqw.between(params.get("beginProductOriginalPrice") != null && params.get("endProductOriginalPrice") != null,
            CommerceProducts::getProductOriginalPrice ,params.get("beginProductOriginalPrice"), params.get("endProductOriginalPrice"));
        lqw.eq(StringUtils.isNotBlank(bo.getProductImage()), CommerceProducts::getProductImage, bo.getProductImage());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), CommerceProducts::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增商品
     */
    @Override
    public Boolean insertByBo(CommerceProductsBo bo) {
        CommerceProducts add = BeanUtil.toBean(bo, CommerceProducts.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setProductId(add.getProductId());
        }
        return flag;
    }

    /**
     * 修改商品
     */
    @Override
    public Boolean updateByBo(CommerceProductsBo bo) {
        CommerceProducts update = BeanUtil.toBean(bo, CommerceProducts.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 修改商品
     */
    @Override
    public Boolean changeStatus(CommerceProductsBo bo) {
        CommerceProducts update = BeanUtil.toBean(bo, CommerceProducts.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CommerceProducts entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
